<?php 

class Mysql{
	private static $obj = null;
	private $config = [
		'host'=>'localhost',
		'user'=>'root',
		'pwd' =>'root',
		'dbname'=>'thinksiteshop',
		'code'=>'utf8'
	];
	private $link = null;
	private $error = "";
	private function __construct($config=[])
	{	
		$this->init($config);
		$this->connect();
	}

	private function __clone()
	{

	}
	private function init($config=[]){
		if(is_array($config)){
			$this->config = array_merge($this->config,$config);
		}
	}
	public function __get($var)
	{
		if(isset($this->config[$var]))
		{
			return $this->config[$var];
		}
	}
	public static function getInstance($config=[])
	{
		if(!self::$obj instanceof self){
			self::$obj = new self($config);
		}
		return self::$obj;
	}

	private function connect()
	{
		$this->link = mysqli_connect($this->host,$this->user,$this->pwd,$this->dbname);
		mysqli_query($this->link,"set names ".$this->code);
	}

	/**
	 *	增删改
	 */

	public function execute($sql)
	{
		//sql语句过滤
		if(strpos(trim($sql),"select")===0){
			$this->error = "执行sql语句类型错误";
			return false;
		}
		if(!mysqli_query($this->link,$sql)){
			$this->error = $this->sqlError();
			return false;
		}
		if(mysqli_affected_rows($this->link)>0){
			return true;
		}else{
			$this->error = "执行失败";
			return false;
		}
	}
	/**
	 *	将查询语句转化二维关联数组
	 */
	public function fetchAll($sql){
		if(strpos(trim($sql),"select")===false){
			$this->error = "sql语句类型不正确";
			return false;
		}
		$res = mysqli_query($this->link,$sql);
		if(!$res){
			$this->error = $this->sqlError();
			return false;
		}
		$infos = mysqli_fetch_all($res,MYSQLI_ASSOC);
		return $infos;
	}

	public function fetch($sql){
		$res = $this->fetchAll($sql);
		if(!$res){
			return false;
		}
		return $res[0];
	}

	public function rowCount($sql){
		$res = $this->fetchAll($sql);
		if(!$res){
			return false;
		}
		return count($res);
	}

	private function sqlError()
	{
		return mysqli_errno($this->link).":".mysqli_error($this->link);
	}
	/*
	 *	获取错误信息
	 */
	public function getError(){
		return $this->error;
	}
}
	$db = Mysql::getInstance();
	$sql = "select * from tk_admin";
	print_r($db->fetchAll($sql));
  	//分页效果
  	$total = $db->rowCount($sql);//总记录数
  	$page = 1;//当前页
  	// 每页记录数
	$every = 2;
	$pages = ceil($total/$every);//总页数
   //判断有没有点击上一页或者下一页
  	if(isset($_GET['page'])){
  		//当前页就是闯过的page的值
  		$page = $_GET['page'];
  	}
  	//当前页如果大于总页数就等于当前页
  	if($page > $pages){
  		$page = $pages;
  	}
  	//当前页小于1时就等于第一页
  	if($page < 1){
  		$page = 1;
  	}// 计算每页的limit的起始位置
  	$start = ($page-1) * $every;
  	$sql = " limit {$start},$every";
  	$db->fetchAll($sql);

?>
<!DOCTYPE HTML >
<html>
 <head>
  <title> 用户中心 </title>
  <meta charset="utf-8">
  <style>
	*{margin:0px;padding:0px}
	table{margin:20px auto}
	.so{width:400px;margin:30px auto}
	.so div{height:40px;line-height:40px}
	.so div span{display:inline-block;width:80px}
	input[name=sub]{width:105px;height:30px}
  </style>
 </head>

 <body>
	<table border=1 cellspacing=0 width="600px">
		<tr height="40px" align="center">
			<td width="7%">编号</td>
			<td width="12%">用户名</td>
			<td width="20%">邮箱</td>
		</tr>
		<?php foreach($db as $vo){?>
		<tr align="center" height="40px">
			<td><?=$vo['admin_id'] ?></td>
			<td><?=$vo['adusername'] ?></td>
			<td><?=$vo['email'] ?></td>
		</tr>
		<?php };?>
		<tr height="40px" align="center">
			<td colspan=6>
				<a href="./Mysql.class.php?page=<?= ($page-1).(isset($url)?$url:"")?>">上一页</a>&nbsp;&nbsp;
				第<?=$page?>页/共<?=$pages ?>页&nbsp;&nbsp;
				<a href="./Mysql.class.php?page=<?= ($page+1).(isset($url)?$url:"")?>">下一页</a>
			</td>
		</tr>
	</table>
 </body>
</html>



